package com.xy.example2;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/14 10:09
 * @description
 */
public class BikeFlyweightFactory {
    // 单例模式 饿汉式
    private static final BikeFlyweightFactory instance = new BikeFlyweightFactory();
    // 创建自行车池
    private Set<BikeFlyweight> pool = new HashSet<>();

    // 放入单车
    private BikeFlyweightFactory() {
        for (int i = 0; i < 2; i++) {
            pool.add(new MoBikeFlyweight(i + 1 + "号"));
        }
    }

    public static BikeFlyweightFactory getInstance() {
        return instance;
    }

    public BikeFlyweight getBike() {
        for (BikeFlyweight bike : pool) {
            if (bike.getState() == 0) {
                return bike;
            }
        }

        return null;
    }
}
